import axios from 'axios'
import { useUserStore } from '../stores/user'

const BASE_URL = 'http://10.162.4.220:8090/api/auth'

const authService = {
    async login(username, password) {
        try {
            const response = await axios.post(`${BASE_URL}/login`, {
                username,
                password
            })

            const { token, id, username: responseUsername } = response.data
            const userStore = useUserStore()

            userStore.setUser(token, id, responseUsername)

            return response
        } catch (error) {
            throw error.response ? error.response.data : error
        }
    }
}

export default authService